package com.coco.goods.service.impl;

import com.coco.goods.mapper.SpecsKeyMapper;
import com.coco.goods.mapper.SpecsMapper;
import com.coco.goods.mapper.SpecsValueMapper;
import com.coco.goods.pojo.TbSpecs;
import com.coco.goods.pojo.TbSpecsKey;
import com.coco.goods.pojo.TbSpecsValue;
import com.coco.goods.service.SpecsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author minicoco
 * @version 1.0
 * @date 2021/7/16 20:39
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SpecsServiceImpl implements SpecsService {

    @Autowired(required = false)
    private SpecsMapper specsMapper;
    @Autowired(required = false)
    private SpecsKeyMapper keyMapper;
    @Autowired(required = false)
    private SpecsValueMapper valueMapper;

    /**
     * 查询所有商品规格信息
     *
     * @return
     */
    @Override
    public List<TbSpecs> findAll() {
        //specs列表
        List<TbSpecs> specsList = specsMapper.findAll();
        //查询下属商品规格
        specsList.forEach(item -> {
            List<TbSpecsKey> keyList = keyMapper.findByPid(item.getId());
            keyList.forEach(key -> {
                List<TbSpecsValue> valueList = valueMapper.findByPid(key.getId());
                key.setValueList(valueList);
            });
            item.setKeyList(keyList);
        });
        return specsList;
    }

    /**
     * 条件查询
     *
     * @param name
     * @return
     */
    @Override
    public List<TbSpecs> findSpecList(String name) {
        //specs列表
        List<TbSpecs> specsList = specsMapper.findByName(name);
        //查询下属商品规格
        specsList.forEach(item -> {
            List<TbSpecsKey> keyList = keyMapper.findByPid(item.getId());
            keyList.forEach(key -> {
                List<TbSpecsValue> valueList = valueMapper.findByPid(key.getId());
                key.setValueList(valueList);
            });
            item.setKeyList(keyList);
        });
        return specsList;
    }


    /**
     * 添加商品规格
     *
     * @param specs
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSpecs(TbSpecs specs) {
        //1.添加规格名称
        specsMapper.insertSpecs(specs);

        //2.添加商品规格属性
        List<TbSpecsKey> keyList = specs.getKeyList();
        keyList.forEach(item -> {
            item.setPid(specs.getId());
            keyMapper.insertKey(item);
            //3.添加商品规格属性值
            List<TbSpecsValue> valueList = item.getValueList();
            valueList.forEach(value -> {
                value.setPid(item.getId());
                valueMapper.insertValue(value);
            });
        });


    }

    /**
     * 根据id查询商品规格
     *
     * @param id
     * @return
     */
    @Override
    public TbSpecs findSpecsById(Long id) {
        TbSpecs specs = specsMapper.findSpecsById(id);
        if (specs != null) {
            List<TbSpecsKey> keyList = keyMapper.findByPid(id);
            keyList.forEach(item -> {
                List<TbSpecsValue> valueList = valueMapper.findByPid(item.getId());
                item.setValueList(valueList);
            });
            specs.setKeyList(keyList);
        }
        return specs;
    }

    /**
     * 修改商品规格
     *
     * @param specs
     */
    @Override
    public void updateSpecs(TbSpecs specs) {
        //获取修改前的规格信息
        TbSpecs specsById = this.findSpecsById(specs.getId());
        List<TbSpecsKey> keyList = null;
        //删除商品规格和商品规格值表
        if (specsById != null) {
            keyList = specsById.getKeyList();
        }
        if (keyList != null) {
            keyList.forEach(item -> {
                valueMapper.deleteByPid(item.getId());
            });
            keyMapper.deleteByPid(specs.getId());
        }
        //添加更新后的商品规格
        specs.getKeyList().forEach(item -> {
            item.setPid(specs.getId());
            keyMapper.insertKey(item);
            List<TbSpecsValue> valueList = item.getValueList();
            if (valueList != null) {
                valueList.forEach(e -> {
                    e.setPid(item.getId());
                    valueMapper.insertValue(e);
                });
            }
        });
        //修改商品规格
        specsMapper.updateSpecs(specs);
    }

    /**
     * 批量删除网品规格
     *
     * @param ids
     */
    @Override
    public void deleteSpecs(Long[] ids) {

        for (Long id : ids) {
            TbSpecs specsById = this.findSpecsById(id);
            specsById.getKeyList().forEach(item -> {
                valueMapper.deleteByPid(item.getId());
            });
            keyMapper.deleteByPid(id);
            specsMapper.deleteById(id);
        }

    }


}
